Skip to content

Conversation

@LeoJHarris
Copy link
Contributor

@LeoJHarris LeoJHarris commented Jul 16, 2025

*Refactor DefaultUserInterface.cs for Better Error Handling and Code Quality & Prevent Theme-related crashes

The PR essentially says: "I prefer to show a Snackbar, but if your app's theme prevents it, I will gracefully fall back to a Toast and ensure the intended action still takes place, thus maximizing compatibility.

Updated the `Microsoft.Maui.Controls` package version from a variable reference `$(MauiVersion)` to a specific version `9.0.82` in both the `Maui.Android.InAppUpdates.SampleApp.csproj` and `Maui.Android.InAppUpdates.csproj` files.
Updated `Microsoft.Extensions.Logging.Debug` to version `9.0.7` in `Maui.Android.InAppUpdates.SampleApp.csproj`.
In `Maui.Android.InAppUpdates.csproj`, incremented versions of several Xamarin.AndroidX and Google Play App Update packages to:
- `Xamarin.AndroidX.Activity.Ktx` to `1.10.1.2`
- `Xamarin.AndroidX.Collection.Ktx` to `1.5.0.2`
- `Xamarin.AndroidX.Fragment.Ktx` to `1.8.8`
- `Xamarin.Google.Android.Play.App.Update.Ktx` to `2.1.0.15`
- Modified `ShowShortToast` to include null checks and exception handling.
- Refactored `ShowSnackbar` to add fallback to toast and improved error handling for theme-related exceptions.
- Introduced `FallbackToToastWithAction` and `HandleSnackbarFailure` for better organization and readability.
- Added `IsThemeRelated` method to identify theme-related exceptions.
- Added a new "NuGet" PropertyGroup in `Maui.Android.InAppUpdates.csproj` for package ID, description, and tags.
- Replaced hardcoded `Microsoft.Maui.Controls` version with variable `$(MauiVersion)` for improved version management.
- Updated ItemGroup for package references while retaining AndroidX library references.
Modified the parameter documentation for the `ShowSnackbar` method. Removed the note about the `clickHandler` parameter being nullable and replaced it with a simpler description of its purpose.
Copilot AI review requested due to automatic review settings July 16, 2025 09:52
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses theme-related crashes in the Android in-app updates library by adding comprehensive error handling and fallback mechanisms to the UI components. The changes focus on resolving the "Error inflating class android.widget.Button" issue mentioned in the title.

  • Enhanced error handling in ShowShortToast with null checks and exception catching
  • Refactored ShowSnackbar with theme-specific exception handling and toast fallback
  • Updated Android package dependencies to newer versions

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
DefaultUserInterface.cs Added comprehensive error handling, theme-related exception detection, and fallback mechanisms for toast and snackbar operations
Maui.Android.InAppUpdates.csproj Updated AndroidX and Google Play package versions to newer releases
Maui.Android.InAppUpdates.SampleApp.csproj Updated Microsoft.Maui.Controls and logging package versions

Replaced hardcoded version number for the
Microsoft.Maui.Controls package in the project file
`Maui.Android.InAppUpdates.SampleApp.csproj` with
a variable `$(MauiVersion)` for improved version
management.
@LeoJHarris
Copy link
Contributor Author

LeoJHarris commented Jul 16, 2025

@HavenDV github Copilot concerns are probably valid but with the absence of the ability to actually invoke the clickHandler using toast should it auto-trigger or no?

@HavenDV
Copy link
Collaborator

HavenDV commented Jul 16, 2025

I think we use clickHandler only in one place:

        Internal.DefaultUserInterface.ShowSnackbar(
            text: "An update has just been downloaded.",
            actionText: "RESTART",
            clickHandler: static _ => Internal.Handler.AppUpdateManager?.CompleteUpdate());

in this case, if we have issues with UI, it will restart app - so should be ok for users

@HavenDV HavenDV merged commit b731f05 into oscoreio:main Jul 16, 2025
1 check passed
@LeoJHarris LeoJHarris deleted the issue-11 branch July 16, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants